//
//  Tracker.m
//  FirstMatchThree
//
//  Created by Petar on 6/20/15.
//  Copyright (c) 2015 Nestorov Engineering. All rights reserved.
//

#import "Tracker.h"
#import "Globals.h"
#import "Flurry.h"
#import <sys/utsname.h>

@implementation Tracker


+ (void)trackEvent:(NSString *)eventName withParams:(NSDictionary *)params
{
    
    NSDictionary *additionalParams = params ?: @{};
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"dd-MM-yyyy HH:mm:ss ZZZ"];
    
    NSString *currentDateString = [formatter stringFromDate:[NSDate date]];
    NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];
    NSString *username = [[NSUserDefaults standardUserDefaults] valueForKey:USERNAME_KEY] ?: @"No Username";
    NSInteger currentUserMoney = [[NSUserDefaults standardUserDefaults] integerForKey:USER_MONEY_KEY] ?: 0;
    NSDate *registrationDate = [[NSUserDefaults standardUserDefaults] objectForKey:REGISTRATION_DATE_KEY] ?: [NSDate date];
    NSString *registrationDateString = [formatter stringFromDate:registrationDate];
    
    NSMutableDictionary *eventParams = [NSMutableDictionary dictionaryWithDictionary:@{
                                                                                       @"Username" : username,
                                                                                       @"Event Date" : currentDateString,
                                                                                       @"Registration Date" : registrationDateString,
                                                                                       @"Country" : countryCode,
                                                                                       @"Device" : [self deviceType],
                                                                                       @"Current User Money" : @(currentUserMoney),
                                                                                       }];
    [eventParams addEntriesFromDictionary:additionalParams];
    
    [Flurry logEvent:eventName withParameters:eventParams];
    
}


#pragma mark - Helpers
+ (NSString *)deviceType
{
    struct utsname systemInfo;
    uname(&systemInfo);
    
    return [NSString stringWithCString:systemInfo.machine
                              encoding:NSUTF8StringEncoding];
}

@end
